﻿#include<stdio.h>

void test1()
{
	FILE* pf = fopen("data1.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}

	int ch = fgetc(stdin);
	fputc(ch, stdout);

	fclose(pf);
}

void test2()
{

		//假设data1.txt⽂件中存放abcdef
	
			FILE* fp = fopen("data1.txt", "r");
			if (fp == NULL)
			{
				perror("fopen\n");
				return 1;
			}
			int i = 0;
			for (i = 0; i < 10; i++)
			{
				int c = fgetc(fp);
				if (c == EOF)
				{
					if (feof(fp))
						printf("遇到⽂件末尾了\n");
					else if (ferror(fp))
						printf("读取发⽣了错误\n");
				}
				else
				{
					fputc(c, stdout); //使⽤fputc在标准输出流上打印字符
				}
			}
			//不再使⽤⽂件时，需要关闭⽂件
			fclose(fp);
			fp = NULL; //将指针置为NULL,避免成为野指针。
		
	}

void test3()
{
	FILE* pf = fopen("data1.txt", "w+");
	fputs("Hello,World!", pf);
	char  tmp[100];
	
	//fseek(pf, 0, SEEK_SET);
	rewind(pf);
	fgets(tmp, 20, pf);
	
	fputs(tmp, stdout);
}

int main()
{
	//test1();
	//test2();
	test3();

}





